package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.RouteDto;
import cn.th.entity.Dormitory;
import cn.th.entity.School;
import cn.th.service.ISchoolService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学校 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询学校
     * @param page
     * @param pageSize
     * @param school
     * @return
     */
    @GetMapping("/list")
    public R<Map<String, Object>> page(@RequestParam(value = "page") int page,
                                       @RequestParam(value = "pageSize") int pageSize,
                                       @RequestParam(value = "school", required = false) String school){

        Map<String, Object> data = schoolService.querySchool(page,pageSize,school);
        return R.success(data);
    }

    /**
     * 新增学校，使用excel表格导入
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> uploadExcel(@RequestParam("file") MultipartFile file) {
        schoolService.saveFile(file);
        return R.success("新增学校成功！！！");
    }

    /**
     * 新增学校，使用普通方法
     * @param school
     * @return
     */
    @PostMapping()
    public R<String> save(@RequestBody School school) {
        schoolService.save(school);
        return R.success("新增学校成功！！！");
    }

    /**
     * 删除学校，可批量删除
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(String ids){
        schoolService.removeBatchByIds(ids);
        return R.success("删除学校成功");
    }

    /**
     * 修改学校
     * @param school
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody School school){

        log.info(school.toString());

        schoolService.updateById(school);
        return R.success("修改学校成功");
    }

    /**
     * 修改学校时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<School> getSchoolById(@PathVariable("id")Long id){
        School school = schoolService.getById(id);
        return R.success(school);
    }
}

